import 'package:flutter/material.dart';
import 'package:cream_chat_flutter/configs/HiColor.dart';
import 'package:cream_chat_flutter/consts/Consts.dart';
import 'package:cream_chat_flutter/utils/HiCache.dart';

extension ThemeModeExtension on ThemeMode {
  String get value => <String>["System", "Light", "Dark"][index];
}

class ThemeProvider with ChangeNotifier {
  ThemeMode _themeMode;

  ThemeMode getThemeMode() {
    String theme = HiCache.getInstance().get(Consts.theme);
    switch (theme) {
      case "System":
        _themeMode = ThemeMode.system;
        break;
      case "Light":
        _themeMode = ThemeMode.light;
        break;
      case "Dark":
        _themeMode = ThemeMode.dark;
        break;
      default:
        _themeMode = ThemeMode.system;
    }
    return _themeMode;
  }

  void setThemeMode(ThemeMode themeMode) {
    HiCache.getInstance().setString(Consts.theme, themeMode.value);
    notifyListeners();
  }

  ThemeData getTheme({bool isDarkMode = false}) => ThemeData(
        brightness: isDarkMode ? Brightness.dark : Brightness.light,
        errorColor: isDarkMode ? HiColor.dark_red : HiColor.light_red,
        primaryColor: isDarkMode ? HiColor.dark_bg : HiColor.light_bg,
        accentColor: isDarkMode ? primary[50] : white,
        indicatorColor: isDarkMode ? primary[50] : white,
        scaffoldBackgroundColor:
            isDarkMode ? HiColor.dark_bg : HiColor.light_bg,
        backgroundColor: isDarkMode ? HiColor.dark_bg : HiColor.light_bg,
      );

  bool isDarkTheme() => _themeMode == ThemeMode.dark;

  Color getPrimaryTextColor() =>
      isDarkTheme() ? Colors.white : Color(0xFF444444);

  Color getBgColor() => isDarkTheme() ? Colors.black : Colors.white;

  double getLgFont() => 18;

  double getNmFont() => 14;

  double getSmFont() => 12;

  Radius getLgRadius() => Radius.circular(26);

  Radius getNmRadius() => Radius.circular(10);

  Radius getSmRadius() => Radius.circular(4);

  String _PingFangRegular = "PingFangRegular";

  TextStyle getSmGreyTextStyle() =>
      TextStyle(fontSize: getSmFont(), color: Colors.grey);

  TextStyle getNmGreyTextStyle() =>
      TextStyle(fontSize: getNmFont(), color: Colors.grey);

  TextStyle getSmPrimaryTextStyle() =>
      TextStyle(fontSize: getSmFont(), color: getPrimaryTextColor());

  TextStyle getNmPrimaryTextStyle() =>
      TextStyle(fontSize: getNmFont(), color: getPrimaryTextColor());

  TextStyle getLgPrimaryTextStyle() =>
      TextStyle(fontSize: getLgFont(), color: getPrimaryTextColor());

  TextStyle getNmBlackTextStyle() =>
      TextStyle(fontSize: getNmFont(), color: Colors.black);

  TextStyle getLgBlackTextStyle() =>
      TextStyle(fontSize: getLgFont(), color: Colors.black);

  Text SmTip(String data) => Text(data, style: getSmGreyTextStyle());

  Text NmTip(String data) => Text(data, style: getNmGreyTextStyle());

  Text SmText(String data) => Text(data, style: getSmPrimaryTextStyle());

  Text NmText(String data) => Text(data, style: getNmPrimaryTextStyle());

  Text LgText(String data) => Text(data, style: getLgPrimaryTextStyle());

  Text Title(String data, {TextStyle style}) {
    if (style == null) {
      style = TextStyle(
          fontSize: getLgFont(),
          fontWeight: FontWeight.bold,
          color: isDarkTheme() ? Colors.white : Colors.black);
    }
    return Text(data, style: style);
  }

  Text SubTitle(String data, {TextStyle style}) {
    if (style == null) {
      style = TextStyle(
          fontSize: getNmFont(),
          fontWeight: FontWeight.bold,
          color: isDarkTheme() ? Colors.white : Colors.black);
    }
    return Text(data,
        style: style);
  }

  Text EllipsisText(String data, {int maxLines = 1, TextStyle style}) {
    if (style == null) {
      style = getNmPrimaryTextStyle();
    }
    return Text(
      data,
      overflow: TextOverflow.ellipsis,
      maxLines: maxLines,
      style: style,
    );
  }

  BoxDecoration BgBoxDecoration() =>
      BoxDecoration(color: isDarkTheme() ? HiColor.dark_bg : HiColor.light_bg);

  BoxDecoration NmBoxDecoration() => BoxDecoration(
      color: getBgColor(), borderRadius: BorderRadius.all(getNmRadius()));

  double getPagePadding() => 20;

  double getItemMargin() => 8;

  Color getGenderColor(int gender) =>
      gender == 2 ? Color(0xFFFF4589) : Colors.blue;
}
